﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Regions;
using Sautom.ClientComunication;
using Sautom.ClientComunication.CommandService;
using Sautom.ClientComunication.QueriesService;
using Sautom.ClientComunication.Services;

namespace Sautom.Modules.Proposal.ViewModels
{
    public sealed class RateManagementViewModel : NavigationAwareNotificationObject
    {
        #region Constructor
        public RateManagementViewModel(IRegionManager regionManager, ServiceFactory serviceFactory)
        {
            RegionManager = regionManager;

            QueriesServiceClient = serviceFactory.GetQueriesService();
            CommandServiceClient = serviceFactory.GetCommandsService();

            AddRateCommand = new DelegateCommand(ExecuteAddRateCommand, () => Date.HasValue && RateValue != 0 && Date.Value >= DisplayStartDate);
            BackCommand = new DelegateCommand(() => RegionManager.RequestNavigate(RegionProvider.MainRegion, PathProvider.ProposalIndex));
        }

        #endregion

        #region Commands
        public DelegateCommand AddRateCommand { get; private set; }
        public DelegateCommand BackCommand { get; private set; }

        private void ExecuteAddRateCommand()
        {
            if (CommandServiceClient.EditOrAddRate(new RateItemDtoInput() { Date = Date.GetValueOrDefault(), RateValue = RateValue }))
            {
                RegionManager.RequestNavigate(RegionProvider.MainRegion, PathProvider.RateManagement);
            }
        }

        #endregion

        #region Properties
        private IRegionManager RegionManager { get; set; }
        private IQueriesService QueriesServiceClient { get; set; }
        private ICommandService CommandServiceClient { get; set; }
        #endregion

        #region ViewModel Properties

        public DateTime DisplayStartDate
        {
            get
            {
                return RateItems.Select(rec => rec.Date).OrderByDescending(rec => rec).FirstOrDefault().AddDays(1);
            }
        }

        private DateTime? _date;
        public DateTime? Date
        {
            get { return _date; }
            set
            {
                _date = value;
                RaisePropertyChanged(() => Date);
                AddRateCommand.RaiseCanExecuteChanged();
            }
        }

        private float _rateValue;
        public float RateValue
        {
            get { return _rateValue; }
            set
            {
                _rateValue = value;
                RaisePropertyChanged(() => RateValue);
                AddRateCommand.RaiseCanExecuteChanged();
            }
        }

        private ObservableCollection<RateItemDtoOutput> _rateItems;
        public ObservableCollection<RateItemDtoOutput> RateItems
        {
            get { return _rateItems; }
            set
            {
                _rateItems = value;
                RaisePropertyChanged(() => RateItems);
            }
        }

        #endregion

        #region INavigationAware

        public override void OnNavigatedTo(NavigationContext navigationContext)
        {
            base.OnNavigatedTo(navigationContext);

            RateItems = new ObservableCollection<RateItemDtoOutput>(QueriesServiceClient.GetRatesList().OrderByDescending(rec => rec.Date));
        }
        #endregion
    }
}
